<template>
  <div>
    <a-menu
      :selectedKeys="selectedKeys"
      :openKeys.sync="openKeys"
      :mode="menuLayout"
      :theme="theme"
      style="height: 64px;line-height: 64px;"
    >
      <template v-for="item in menuData">
        <a-menu-item
          v-if="!item.children"
          :key="item.path"
          @click="() => $router.push({ path: item.path, query: $route.query })"
        >
          <a-icon v-if="item.meta.icon" :type="item.meta.icon" />
          <span>{{ item.meta.title }}</span>
        </a-menu-item>
        <sub-menu v-else :key="item.path" :menu-info="item" />
      </template>
    </a-menu>
  </div>
</template>

<script>
import SubMenu from "./SubMenu";
import { check } from "../utils/auth";

export default {
  props: {
    theme: {
      type: String,
      default: "dark"
    },
    menuLayout: {
      type: String,
      default: "inline"
    },
    layoutCollapsed: {
      type: Boolean,
      default: false
    }
  },
  components: {
    "sub-menu": SubMenu
  },
  watch: {
    "$route.path": function(val) {
      this.selectedKeys = this.selectedKeysMap[val];
      this.openKeys = this.collapsed ? [] : this.openKeysMap[val];
    },
    layoutCollapsed: function(val) {
      // console.log("菜单收起来了", val);
      if (val === true) {
        this.openKeys = [];
      } else {
        this.openKeys = this.collapsed
          ? []
          : this.openKeysMap[this.$route.path];
      }
    }
  },
  data() {
    this.selectedKeysMap = {};
    this.openKeysMap = {};
    const menuData = this.getMenuData(this.$router.options.routes); // 获取路由配置信息
    return {
      collapsed: false,
      menuData,
      selectedKeys: this.selectedKeysMap[this.$route.path],
      openKeys: this.collapsed
        ? []
        : this.menuLayout === "inline"
        ? this.openKeysMap[this.$route.path]
        : []
    };
  },
  methods: {
    toggleCollapsed() {
      this.collapsed = !this.collapsed;
    },
    /**
     * 根据router配置的路由信息生成菜单列表从而渲染到页面侧边栏上
     *
     * @param {Object} routes 路由信息
     * @returns {Object} 返回层级菜单列表
     */
    getMenuData(routes = [], parentKeys = [], selectedKeys) {
      const menuData = [];
      // routes.forEach(item => {
      for (let item of routes) {
        if (item.meta && item.meta.authority && !check(item.meta.authority)) {
          // break;
          continue;
        }
        if (item.name && !item.hideInMenu) {
          this.openKeysMap[item.path] = parentKeys;
          this.selectedKeysMap[item.path] = [selectedKeys || item.path];
          const newItem = { ...item };
          delete newItem.children;
          if (item.children && !item.hideChildrenInMenu) {
            newItem.children = this.getMenuData(item.children, [
              ...parentKeys,
              item.path
            ]);
          } else {
            this.getMenuData(
              item.children,
              selectedKeys ? parentKeys : [...parentKeys, item.path],
              selectedKeys || item.path
            );
          }
          menuData.push(newItem);
        } else if (
          !item.hideInMenu &&
          !item.hideChildrenInMenu &&
          item.children
        ) {
          menuData.push(
            ...this.getMenuData(item.children, [...parentKeys, item.path])
          );
        }
      }
      return menuData;
    }
  }
};
</script>
